package com.softaria.promosite.client;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.softaria.windows.config.MenuConfiguration;
import com.softaria.windows.factories.DefaultViewFactory;
import com.softaria.windows.logic.URLParser;
import com.softaria.windows.logic.WindowsManager;
import com.softaria.windows.model.ModelUrl;
import com.softaria.windows.seo.DefaultPerspectiveUrlBuilder;
import com.softaria.windows.seo.HttpParametersConstants;
import com.softaria.windows.seo.LocationParameterSet;
import com.softaria.windows.widgets.Desktop;

public class EntryPointImpl implements EntryPoint, HttpParametersConstants {

  private static WindowsManager wm;

  public void onModuleLoad() {

    // Tell google bot that we are honest
    // Initially div with static content is hidden to not confuse users
    DOM.getElementById("aria_content").setAttribute("visibility", "visible");

    final DefaultViewFactory viewFactory = new DefaultViewFactory() {
      @Override
      protected String getViewType(String viewId, Map<String, List<String>> params) {
        // calculate view type from parameters:
        return viewId;
      }

    };

    // Get menu
    ContentServiceAsync contentService = ContentServiceProvider.getContentService();

    contentService.getMenuConfiguration(new AsyncCallback<MenuConfiguration>() {

      public void onFailure(Throwable caught) {
        caught.printStackTrace();

      }

      public void onSuccess(MenuConfiguration config) {
        // Creating desktop
        Desktop desktop = new Desktop(config);

        // register view type factories
        viewFactory.registerTypedViewFactory(ContentService.TEXT_TYPE, new TextTypedViewFactory(
            config));

        viewFactory.registerTypedViewFactory(ContentService.LISTENERS_DEMO,
            new ListenersSampleTypedViewFactory());

        viewFactory.registerTypedViewFactory(ContentService.HELP_DEMO,
            new HelpDemoTypedViewFactory());

        viewFactory.registerTypedViewFactory(ContentService.WIDGETS_DEMO,
            new ManyWidgetsTypedViewFactory());

        // passing Desktop,menu,guard and viewFactory to WindowsManager
        wm = new WindowsManager(config, desktop, viewFactory, null);

        publish();

        // Build perspective url from normal url and make WindowManager open it
        DefaultPerspectiveUrlBuilder ub = new DefaultPerspectiveUrlBuilder();
        ModelUrl url = ub.buildModelUrl(new LocationParameterSet());

        wm.openPerspectiveOrView(url);

      }

    });

  }

  @SuppressWarnings("unused")
  // Warning is suppressed, because it is used by following native method
  private static void openLink(String historyToken) {
    ModelUrl url = URLParser.parse(historyToken);
    if (url != null) {
      wm.openPerspectiveOrView(url);
    }

  }

  // Set up the JS-callable signature as a global JS function named
  // ariaOpenLink.
  private native void publish() /*-{
         $wnd.ariaOpenLink = 
         @com.softaria.promosite.client.EntryPointImpl::openLink(Ljava/lang/String;);
       }-*/;

}
